package com.example.zzm.yuejian03.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzm.yuejian03.Fragment.ClubFragment;
import com.example.zzm.yuejian03.Fragment.CourseFragment;
import com.example.zzm.yuejian03.Fragment.HomeFragment;
import com.example.zzm.yuejian03.Fragment.MyFragment;
import com.example.zzm.yuejian03.Fragment.PictureFragment;
import com.example.zzm.yuejian03.R;

import java.util.ArrayList;
import java.util.List;

public  class MainActivity extends BaseActivity implements View.OnClickListener
        ,HomeFragment.OnFragmentInteractionListener
        ,CourseFragment.OnFragmentInteractionListener
        ,ClubFragment.OnFragmentInteractionListener
        ,PictureFragment.OnFragmentInteractionListener
        ,MyFragment.OnFragmentInteractionListener
{
    private LinearLayout layout1,layout2,layout3,layout4,layout5;
    private ImageView tab_course,tab_club,tab_home,tab_picture,tab_my;
    private Fragment fragment_course,fragment_club,fragment_home,fragment_picture,fragment_my;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
     private int userid;
    private SharedPreferences sp;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_main);
        layout1=(LinearLayout)findViewById(R.id.layout1);
        layout2=(LinearLayout)findViewById(R.id.layout2);
        layout3=(LinearLayout)findViewById(R.id.layout3);
        layout4=(LinearLayout)findViewById(R.id.layout4);
        layout5=(LinearLayout)findViewById(R.id.layout5);
        tab_course=(ImageView) findViewById(R.id.tab_shop);
        tab_club=(ImageView) findViewById(R.id.tab_club);
        tab_home=(ImageView) findViewById(R.id.tab_home);
        tab_picture=(ImageView) findViewById(R.id.tab_picture);
        tab_my=(ImageView) findViewById(R.id.tab_my);
        setSelect(2);

    }


    @Override
    void initEvent() {
        layout1.setOnClickListener(this);
        layout2.setOnClickListener(this);
        layout3.setOnClickListener(this);
        layout4.setOnClickListener(this);
        layout5.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        //所有图片制成normal的那张
        resetImage();
        //选择哪个，哪个fragment显示出来，图片高亮
        switch (view .getId()){
            case R.id.layout1:
                setSelect(0);
                break;
            case R.id.layout2:
                setSelect(1);
                break;
            case R.id.layout3:
                setSelect(2);
                break;
            case R.id.layout4:
                setSelect(3);
                break;
            case R.id.layout5:
                setSelect(4);
                break;
            default:
                break;
        }
    }
    @Override
    void initData() {
        sp = getSharedPreferences("userInfo",MODE_PRIVATE);
        userid = sp.getInt("user_id",1);
        Toast.makeText(getApplicationContext(),"uid="+Integer.toString(userid),Toast.LENGTH_SHORT).show();
    }

    private void resetImage(){
        tab_course.setImageResource(R.drawable.ic_jianshen);
        tab_club.setImageResource(R.drawable.ic_quguo);
        tab_home.setImageResource(R.drawable.ic_shouye);
        tab_picture.setImageResource(R.drawable.ic_duanxin);
        tab_my.setImageResource(R.drawable.ic_geren);
    }

    private void setSelect(int i){
        fragmentManager=getSupportFragmentManager();
        fragmentTransaction=fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch(i){
            case 0:
                if(fragment_course==null){
                    fragment_course=new CourseFragment();
                    fragmentTransaction.add(R.id.content,fragment_course);
                }
                else{
                    fragmentTransaction.show(fragment_course);
                }
                tab_course.setImageResource(R.drawable.ic_xingcheng);
                break;
            case 1:
                if(fragment_club==null){
                    fragment_club=new ClubFragment();
                    fragmentTransaction.add(R.id.content,fragment_club);
                }
                else{
                    fragmentTransaction.show(fragment_club);
                }
                tab_club.setImageResource(R.drawable.ic_quguo_fill);
                break;
            case 2:
                if(fragment_home==null){
                    fragment_home=new HomeFragment();
                    fragmentTransaction.add(R.id.content,fragment_home);
                }
                else{
                    fragmentTransaction.show(fragment_home);
                }
                tab_home.setImageResource(R.drawable.ic_shouye_fill);
                break;
            case 3:
                if(fragment_picture==null){
                    fragment_picture=new PictureFragment();
                    fragmentTransaction.add(R.id.content,fragment_picture);
                }
                else{
                    fragmentTransaction.show(fragment_picture);
                }
                tab_picture.setImageResource(R.drawable.ic_duanxin_fill);
                break;
            case 4:
                if(fragment_my==null){
                    fragment_my=new MyFragment();
                    fragmentTransaction.add(R.id.content,fragment_my);
                }
                else{
                    fragmentTransaction.show(fragment_my);
                }
                tab_my.setImageResource(R.drawable.ic_geren_fill);
                break;
            default:
                break;
        }
        fragmentTransaction.commit();//提交
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if(fragment_club!=null){
            fragmentTransaction.hide(fragment_club);
        }
        if(fragment_course!=null){
            fragmentTransaction.hide(fragment_course);
        }
        if(fragment_home!=null){
            fragmentTransaction.hide(fragment_home);
        }
        if(fragment_my!=null){
            fragmentTransaction.hide(fragment_my);
        }if(fragment_picture!=null){
            fragmentTransaction.hide(fragment_picture);
        }
    }
}

